﻿#region Using directives
using System.ComponentModel;
using System.Linq;
using System.Web.Script.Services;
using System.Web.Services;
using System.Collections.Generic;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for InterestService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class InterestService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public void DelInterestsFromUser(int userId)
        {
            var rowstoDelete =
                    from i in db.UserInterests
                    where i.UserId == userId
                    select i;

            foreach (var row in rowstoDelete)
            {
                db.UserInterests.DeleteOnSubmit(row);
            }

            try
            {
                db.SubmitChanges();
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e);
                // Provide for exceptions.
            }


        }
        
        [WebMethod]
        public void AddInterestToUser(int userId, int interestId) {
            ProcolServices.UserInterest userInterest = new ProcolServices.UserInterest();
            userInterest.UserId = userId;
            userInterest.InterestId = interestId;

            db.UserInterests.InsertOnSubmit(userInterest);

            try
            {
                db.SubmitChanges();
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e);
            }

            
        }

        [WebMethod]
        public CategoryBean[] GetCategoriesList(int languageId)
        {
            List<CategoryBean> listCategories = new List<CategoryBean>();
            var query = from i in db.Categories
                        select new
                        {
                            i.CategoryId,
                        };
            foreach (var q in query)
            {
                var query2 = from i in db.CategoryNames
                             where i.CategoryId == q.CategoryId && i.LanguageId == languageId
                             select new
                             {
                                 i.CategoryId,
                                 i.Name
                             };
                listCategories.Add(new CategoryBean
                {
                    categoryId = query2.First().CategoryId,
                    name = query2.First().Name,
                    interests = GetInterestsList(languageId, query2.First().CategoryId)
                });
            }
            return listCategories.ToArray();

        }

        [WebMethod]
        public InterestBean[] GetInterestsList(int languageId, int categoryId)
        {
            List<InterestBean> listInterests = new List<InterestBean>();
            var query = from i in db.Interests
                        where i.CategoryId == categoryId
                        select new
                        {
                            i.InterestId,
                        };
            foreach (var q in query)
            {
                var query2 = from i in db.InterestNames
                             where i.InterestId == q.InterestId && i.LanguageId == languageId
                             select new
                             {
                                 i.InterestId,
                                 i.Name
                             };
                listInterests.Add(new InterestBean
                {
                    interestId = query2.First().InterestId,
                    name = query2.First().Name
                });
            }
            return listInterests.ToArray();

        }

        [WebMethod]
        public int[] GetUserInterests(int userId)
        {
            List<int> interestsId = new List<int>();
            var query = from i in db.UserInterests where i.UserId == userId
                        select new
                        {
                            i.InterestId,
                        };
            foreach (var q in query)
            {
                interestsId.Add(q.InterestId);
            }
            return interestsId.ToArray();

        }

    }

    #region Nested type: InterestBean
    public class InterestBean
    {
        public int interestId { get; set; }
        public string name { get; set; }
    }
    #endregion

    #region Nested type: CategoryBean
    public class CategoryBean
    {
        public int categoryId { get; set; }
        public string name { get; set; }
        public InterestBean[] interests { get; set; }
    }
    #endregion
}